package com.qianfeng.gl4study.youshare.acitivity.common;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.qianfeng.gl4study.youshare.R;
import com.qianfeng.gl4study.youshare.acitivity.social.GroupMainActivity;
import com.qianfeng.gl4study.youshare.acitivity.store.CollectActivity;
import com.qianfeng.gl4study.youshare.acitivity.store.StoreActivity;
import com.qianfeng.gl4study.youshare.adapters.CommonImagePagerAdapter;
import com.qianfeng.gl4study.youshare.adapters.GridViewBaseAdapter;
import com.qianfeng.gl4study.youshare.utils.Constants;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 主界面
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/4
 * Email:pdsfgl@live.com
 */
public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener, AdapterView.OnItemClickListener {

    private RadioGroup radioGroup;
    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //去掉ActionBar的图标，标题居中
        ActionBar actionBar = getActionBar();
        if(actionBar!=null){
            //设置透明图，吐掉图标
            actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));
            //设置标题居中
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setCustomView(R.layout.actionbar_main_title);//设置默认的标题。

        }

        viewPager = (ViewPager) findViewById(R.id.main_poster);
        radioGroup = (RadioGroup) findViewById(R.id.main_poster_switch);
        if(viewPager !=null){
            //设置Adapter
            List<Integer> imageIds = new LinkedList<Integer>();
            imageIds.add(R.drawable.main_image1);
            imageIds.add(R.drawable.main_image2);
            imageIds.add(R.drawable.main_image3);
            imageIds.add(R.drawable.main_image4);
            imageIds.add(R.drawable.main_image5);
            CommonImagePagerAdapter adapter = new CommonImagePagerAdapter(imageIds, this);
            viewPager.setAdapter(adapter);
            radioGroup.check(R.id.rb_1);//开始默认打开第一个页面，单选按钮选择第一个
            viewPager.setOnPageChangeListener(this);
            radioGroup.setOnCheckedChangeListener(this);
        }

        doGridViewUI(); //完成九宫格布局

    }

//==============监听单选按钮点击事件=============================
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId){
            case R.id.rb_1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rb_2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rb_3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.rb_4:
                viewPager.setCurrentItem(3);
                break;
            case R.id.rb_5:
                viewPager.setCurrentItem(4);
                break;
        }

    }
//=============================================

//=====================监听ViewPager的滚动事件=============================================
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        switch (position){
            case 0:
                radioGroup.check(R.id.rb_1);
                break;
            case 1:
                radioGroup.check(R.id.rb_2);
                break;
            case 2:
                radioGroup.check(R.id.rb_3);
                break;
            case 3:
                radioGroup.check(R.id.rb_4);
                break;
            case 4:
                radioGroup.check(R.id.rb_5);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

//==================================================================

    /**
     * 九宫格布局实现代码
     */
    private void doGridViewUI() {
        GridView gridView = (GridView) findViewById(R.id.main_entry);
        if(gridView!=null){
            LinkedList<Map<String, ?>> data = new LinkedList<Map<String, ?>>();
            HashMap<String, Integer> map = new HashMap<String, Integer>();

            map.put("bg",R.drawable.ico_yingshi_bg);
            map.put("icon",R.drawable.ico_yingshi);
            map.put("title",R.string.main_video);
            data.add(map);

            map = new HashMap<String, Integer>();
            map.put("bg",R.drawable.ico_shangcheng_bg);
            map.put("icon",R.drawable.ico_shangcheng);
            map.put("title",R.string.main_store);
            data.add(map);

            map = new HashMap<String, Integer>();
            map.put("bg",R.drawable.ico_xianchang_bg);
            map.put("icon",R.drawable.ico_xianchang);
            map.put("title",R.string.main_room);
            data.add(map);

            map = new HashMap<String, Integer>();
            map.put("bg",R.drawable.ico_shejiao_bg);
            map.put("icon",R.drawable.ico_shejiao);
            map.put("title",R.string.main_person);
            data.add(map);

            map = new HashMap<String, Integer>();
            map.put("bg",R.drawable.ico_user_bg);
            map.put("icon",R.drawable.ico_user);
            map.put("title",R.string.main_sns);
            data.add(map);

            map = new HashMap<String, Integer>();
            map.put("bg",R.drawable.ico_setting_bg);
            map.put("icon",R.drawable.ico_setting);
            map.put("title",R.string.main_setting);
            data.add(map);

            gridView.setAdapter(new GridViewBaseAdapter(this, data,
                    R.layout.iten_main_enter_point));
            gridView.setOnItemClickListener(this);
        }
    }

    /**
     * 九宫格点击事件
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = null;
        switch (position){//仅实现商城的点击，无论点击哪个都进入商城
            case 0:
            case 1:
            case 2:


            case 5:
                intent = new Intent(this,StoreActivity.class);
                startActivity(intent);
                break;
            case 3:
                intent = new Intent(this, CollectActivity.class);
                if(null== Constants.USER_ID||"".equals(Constants.USER_ID)) {
                    Toast.makeText(this,"请先登录",Toast.LENGTH_LONG).show();
                }else {
                    startActivity(intent);
                }
                break;
            case 4:
                intent = new Intent(this, GroupMainActivity.class);
                startActivity(intent);
                break;
        }


    }



    /**
     * 加载菜单ActionBar
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.main, menu);

        return true;
    }

    /**
     * 进行登录和注册的跳转
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 获取菜单的ID, 就是 <item android:id 这个属性
        int menuId = item.getItemId();

        String msg = "无内容";
        Intent intent = null;
        switch (menuId) {

            case R.id.action_login:     //进行登录操作
                intent = new Intent(this,LoginActivity.class);
                break;
            case R.id.action_register:  //进行组册操作
                intent = new Intent(this,RegisterActivity.class);
                break;
        }

        if(intent!=null){
            startActivity(intent);
        }
        return true;
    }

    private long lastBackTime = 0;//上次点击返回键的时间
    /**
     * 当前Activity显示的时候，点击屏幕上的返回键的事件处理
     */
    @Override
    public void onBackPressed() {
        //获取当前时间的毫秒数，与上次点击返回键的时间比较，相差2秒以内退出程序
        long currentTimeMillis = System.currentTimeMillis();
        if(currentTimeMillis - lastBackTime <= 2000){
            //结束程序，使用super.onBackPressed()让Activity自己处理返回键，或者使用finish（）。
            finish();
            //终止整个应用程序的进程，所有进程终止，包括Service的服务。
            //System.exit(0);

        }else {
            Toast.makeText(this,"再次点击退出",Toast.LENGTH_LONG).show();
            lastBackTime = currentTimeMillis;
        }
    }



}
